import 'dart:io';

import 'package:auto_route/auto_route.dart';
import 'package:base/main.dart';
import 'package:flutter/material.dart';
import 'package:framework/core/base_view.dart';
import 'package:module_home/pages/home/home_logic.dart';
import 'package:module_home/pages/home/widgets/home_banner_widget.dart';
import 'package:module_home/pages/home/widgets/home_domain_widget.dart';
import 'package:module_home/pages/home/widgets/home_finance_widget.dart';
import 'package:module_home/pages/home/widgets/home_function_widget.dart';
import 'package:module_home/pages/home/widgets/home_notice_widget.dart';
import 'package:module_home/pages/home/widgets/home_quick_widget.dart';
import 'package:module_home/pages/home/widgets/home_search_widget.dart';

@RoutePage()
class HomePage extends BaseView<HomeLogic> {
  HomePage({super.key});

  @override
  HomeLogic createLogic() => HomeLogic();
  final ScrollController _scrollController = ScrollController();

  @override
  initState() {
    _scrollController.addListener(() {
      final logic = Get.find<HomeLogic>();
      final offset = _scrollController.offset;
      final isScrolling =
          _scrollController.position.activity is! HoldScrollActivity;
      logic.bannerVM.updateScrollState(offset, isScrolling);
    });
  }

  @override
  dispose() {
    super.dispose();
  }

  @override
  Widget buildView(BuildContext context, HomeLogic logic) {
    return Scaffold(body: _buildCustomScrollView(context, logic));
  }

  Widget _buildCustomScrollView(BuildContext context, HomeLogic logic) {
    return Stack(
      children: [
        _buildGlobalBackground(),
        CustomScrollView(
          controller: _scrollController,
          slivers: [
            _buildSliverAppBar(context, logic),
            SliverList(
                delegate: SliverChildListDelegate([
              ///数据统计
              HomeDomainWidget(logic: logic),
              _buildRoundedWidget(),

              /// 中部区域
              _buildContentWidget(logic),
            ]))
          ],
        ),
      ],
    );
  }

  Widget _buildGlobalBackground() {
    return Positioned.fill(
      child: Container(
        decoration: const BoxDecoration(
          gradient: RadialGradient(
            colors: [Color(0xFF1A81EF), Color(0xFF1262E9)],
            center: Alignment.topLeft,
            radius: 1.5,
          ),
        ),
      ),
    );
  }

  _buildSliverAppBar(BuildContext context, HomeLogic logic) {
    return SliverAppBar(
      pinned: true,
      expandedHeight: 120,
      toolbarHeight: 88,
      backgroundColor: Colors.transparent,
      leading: null,
      automaticallyImplyLeading: false,
      bottom: PreferredSize(
        preferredSize: Size.fromHeight(Platform.isIOS ? -12 : -6),
        child: SizedBox(),
      ),
      actions: [
        Icon(Icons.settings_phone_rounded, color: Colors.white, size: 16),
        Container(
          margin: EdgeInsets.symmetric(horizontal: 16),
          child: Icon(Icons.add, color: Colors.white, size: 16),
        )
      ],
      flexibleSpace: Obx(() {
        final progress =
            (logic.bannerVM.scrollOffset.value / 120).clamp(0.0, 1.0);
        var widthPro = 1 - progress;
        if (widthPro <= 0.8) {
          widthPro = 0.8;
        }
        return Container(
          decoration: BoxDecoration(
            gradient: RadialGradient(
              colors: [
                Color(0xFF1A81EF)
                    .withOpacity(logic.bannerVM.isScrolling.value ? 1 : 0),
                Color(0xFF1262E9).withOpacity(progress),
              ],
              center: Alignment.topLeft,
              radius: 1.2 + (1 - progress) * 0.3,
            ),
          ),
          child: FlexibleSpaceBar(
            centerTitle: false,
            collapseMode: CollapseMode.pin,
            expandedTitleScale: 1,
            title: ConstrainedBox(
              constraints: BoxConstraints(
                  maxWidth: MediaQuery.of(context).size.width * widthPro,
                  minHeight: 32,
                  maxHeight: 88),
              child: HomeSearchWidget(progress: progress),
            ),
            background: Container(color: Colors.transparent),
          ),
        );
      }),
    );
  }

  _buildRoundedWidget() {
    return Container(
      padding: EdgeInsets.only(left: 1, right: 1),
      child: Image.asset(
        'assets/images/slices/减去 1@3x.png',
        width: double.infinity,
        color: Colors.white,
      ),
    );
  }

  _buildContentWidget(HomeLogic logic) {
    return Container(
        width: double.infinity,
        margin: const EdgeInsets.only(left: 1, right: 1),
        color: Colors.white,
        constraints: BoxConstraints(
          minHeight: 500,
        ),
        child: Column(
          children: [
            /// 快捷区
            HomeQuickWidget(logic: logic),

            /// 金刚区
            HomeFunctionWidget(vm: logic.functionVM),

            /// 新闻公告
            const HomeNoticeWidget(),

            /// banner
            HomeBannerWidget(vm: logic.bannerVM),

            /// 财务中心
            const HomeFinanceWidget(),
          ],
        ));
  }
}
